package cn.finedo.highway.service;

import cn.finedo.fsdp.service.common.jdbc.JdbcTemplate;
import cn.finedo.highway.domain.LogDomain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @description:定时统计时新增错误日志
 * @author: lizheng
 * @createDate: 2023-2-19
 * @version: 1.0
 */
@Component
public class DataLogService {

    @Resource(name = "fsdpJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 新增数据统计运行日志
     * @param dsid 统计名称
     * @param dsname 目标表名
     * @param statdate 运行日期
     * @param starttime 运行开始时间
     * @param endtime 运行结束时间
     * @param state 运行状态：成功、失败
     * @param statedesc 运行状态描述，如果失败，这里写入Java Exception的异常信息
     */
    public void addDataStatLog(String dsid,String dsname,String columnname,String statdate,String starttime,String endtime,String state,String statedesc){
        StringBuffer logStr = new StringBuffer();
        logStr.append(" dsid:").append(dsid)
                .append(" 运行日期:").append(statdate)
                .append(" 表名:").append(dsname)
                .append(" 字段:").append(columnname)
                .append(" 运行开始时间:").append(starttime)
                .append(" 运行结束时间:").append(endtime)
                .append(" 运行状态:").append(state)
                .append(" 运行状态描述:").append(statedesc);
        logger.info(logStr.toString());
        LogDomain logDomain = new LogDomain();
        logDomain.setDsid(dsid);
        logDomain.setDsname(dsname);
        logDomain.setColumnname(columnname);
        logDomain.setStatdate(statdate);
        logDomain.setStarttime(starttime);
        logDomain.setEndtime(endtime);
        logDomain.setState(state);
        logDomain.setStatedesc(statedesc);
        //拼接SQL
        StringBuffer sql = new StringBuffer();
        sql.append("INSERT into LW_DATASTATLOG( DSID,DSNAME,COLUMNNAME,STATDATE,STARTTIME,ENDTIME,STATE,STATEDESC )")
                .append(" VALUES(:dsid, :dsname, :columnname, :statdate, :starttime, :endtime, :state, :statedesc ) ");
        System.out.println(sql);
        try {
            jdbcTemplate.updateNohis(sql.toString(),logDomain);
        }catch (Exception e){
            logger.error("运行日志新增错误"+e.getMessage());
        }
    }

}
